package com.primeton.esper.main;

import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.espertech.esper.client.EPServiceProvider;
import com.primeton.esper.evententry.OrderEvent;

public class AutoMachine extends Thread {

	private static final Log log = LogFactory.getLog(AutoMachine.class);

	private EPServiceProvider epService;

	public AutoMachine(EPServiceProvider epService) {
		this.epService = epService;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		log.info("开始发送事件！");
		Random random = new Random();
		int num = 1;
		while (num-- > 0) {
			log.info("num : " + num);
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				log.info("Something wrong with the thread!", e);
			}
			sendPatternTest();
			// OrderEvent orderEvent = new OrderEvent("shirt" + num, num + 10);
			// System.out.println(orderEvent.getItemName() + " " + orderEvent.getPrice());
			// epService.getEPRuntime().sendEvent(orderEvent);
		}
		log.info("ok,over!");

	}

	private void sendTTT() {
		OrderEvent orderEvent1 = new OrderEvent("item1", 2.5);
		OrderEvent orderEvent2 = new OrderEvent("item1", 2.2);
		OrderEvent orderEvent3 = new OrderEvent("item3", 2.9);
		OrderEvent orderEvent4 = new OrderEvent("item1", 2.6);
		epService.getEPRuntime().sendEvent(orderEvent1);
		epService.getEPRuntime().sendEvent(orderEvent2);
		epService.getEPRuntime().sendEvent(orderEvent3);
		epService.getEPRuntime().sendEvent(orderEvent4);
	}

	private void sendPatternTest() {
		// TODO Auto-generated method stub
		OrderEvent orderEvent1 = new OrderEvent("item1", 2.5);
		OrderEvent orderEvent2 = new OrderEvent("item2", 2.2);
		OrderEvent orderEvent3 = new OrderEvent("item1", 2.9);
		OrderEvent orderEvent4 = new OrderEvent("item3", 2.4);
		OrderEvent orderEvent5 = new OrderEvent("item1", 3.4);
		OrderEvent orderEvent6 = new OrderEvent("item2", 1.3);
		OrderEvent orderEvent7 = new OrderEvent("item3", 2.9);
		OrderEvent orderEvent8 = new OrderEvent("item1", 2.8);
		OrderEvent orderEvent9 = new OrderEvent("item3", 2.5);
		OrderEvent orderEvent10 = new OrderEvent("item1", 1.8);
		OrderEvent orderEvent11 = new OrderEvent("item3", 2.5);
		OrderEvent orderEvent12 = new OrderEvent("item1", 3.5);

		OrderEvent orderEvent21 = new OrderEvent("item1", 2.5);
		OrderEvent orderEvent22 = new OrderEvent("item2", 2.12);
		OrderEvent orderEvent23 = new OrderEvent("item1", 2.91);
		OrderEvent orderEvent24 = new OrderEvent("item3", 2.4);
		OrderEvent orderEvent25 = new OrderEvent("item1", 3.4);
		OrderEvent orderEvent26 = new OrderEvent("item2", 1.3);
		OrderEvent orderEvent27 = new OrderEvent("item3", 2.9);
		OrderEvent orderEvent28 = new OrderEvent("item1", 2.8);
		OrderEvent orderEvent29 = new OrderEvent("item3", 2.5);
		OrderEvent orderEvent210 = new OrderEvent("item1", 1.8);
		OrderEvent orderEvent211 = new OrderEvent("item3", 2.5);
		OrderEvent orderEvent212 = new OrderEvent("item1", 3.5);

		OrderEvent orderEvent31 = new OrderEvent("item1", 2.5);
		OrderEvent orderEvent32 = new OrderEvent("item2", 2.2);
		OrderEvent orderEvent33 = new OrderEvent("item1", 2.9);
		OrderEvent orderEvent34 = new OrderEvent("item3", 2.4);
		OrderEvent orderEvent35 = new OrderEvent("item1", 3.4);
		OrderEvent orderEvent36 = new OrderEvent("item2", 1.3);
		OrderEvent orderEvent37 = new OrderEvent("item3", 2.9);
		OrderEvent orderEvent38 = new OrderEvent("item1", 2.8);
		OrderEvent orderEvent39 = new OrderEvent("item3", 2.5);
		OrderEvent orderEvent310 = new OrderEvent("item1", 1.8);
		OrderEvent orderEvent311 = new OrderEvent("item3", 2.5);
		OrderEvent orderEvent312 = new OrderEvent("item1", 3.5);

		OrderEvent ttt = new OrderEvent("item3", 2.6);
		OrderEvent[] events = new OrderEvent[] { orderEvent1,
				orderEvent2,
				orderEvent3,
				orderEvent4,
				orderEvent5,
				orderEvent6,
				orderEvent7,
				orderEvent8,
				orderEvent9,
				orderEvent10,
				orderEvent11,
				orderEvent12,

				orderEvent21,
				orderEvent22,
				orderEvent23,
				orderEvent24,
				orderEvent25,
				orderEvent26,
				orderEvent27,
				orderEvent28,
				orderEvent29,
				orderEvent210,
				orderEvent211,
				orderEvent212,

				orderEvent31,
				orderEvent32,
				orderEvent33,
				orderEvent34,
				orderEvent35,
				orderEvent36,
				orderEvent37,
				orderEvent38,
				orderEvent39,
				orderEvent310,
				orderEvent311,
				orderEvent312,
		};
		for (OrderEvent temp : events) {
			// String t;
			// t = "item1".equals(temp.getItemName()) ? "+++++++++++++++++" : "item2".equals(temp.getItemName()) ?
			// "--------" : "***";
			// System.out.println(t + ":" + temp.getPrice());
			epService.getEPRuntime().sendEvent(temp);
		}
		/*
				epService.getEPRuntime().sendEvent(orderEvent1);
				epService.getEPRuntime().sendEvent(orderEvent2);
				epService.getEPRuntime().sendEvent(orderEvent3);
				epService.getEPRuntime().sendEvent(orderEvent4);
				epService.getEPRuntime().sendEvent(orderEvent5);
				epService.getEPRuntime().sendEvent(orderEvent6);
				epService.getEPRuntime().sendEvent(orderEvent7);
				epService.getEPRuntime().sendEvent(orderEvent8);
				epService.getEPRuntime().sendEvent(orderEvent9);
				epService.getEPRuntime().sendEvent(orderEvent10);
				epService.getEPRuntime().sendEvent(orderEvent11);
				epService.getEPRuntime().sendEvent(orderEvent12);

				epService.getEPRuntime().sendEvent(orderEvent21);
				epService.getEPRuntime().sendEvent(orderEvent22);
				epService.getEPRuntime().sendEvent(orderEvent23);
				epService.getEPRuntime().sendEvent(orderEvent24);
				epService.getEPRuntime().sendEvent(orderEvent25);
				epService.getEPRuntime().sendEvent(orderEvent26);
				epService.getEPRuntime().sendEvent(orderEvent27);
				epService.getEPRuntime().sendEvent(orderEvent28);
				epService.getEPRuntime().sendEvent(orderEvent29);
				epService.getEPRuntime().sendEvent(orderEvent210);
				epService.getEPRuntime().sendEvent(orderEvent211);
				epService.getEPRuntime().sendEvent(orderEvent212);

				epService.getEPRuntime().sendEvent(orderEvent31);
				epService.getEPRuntime().sendEvent(orderEvent32);
				epService.getEPRuntime().sendEvent(orderEvent33);
				epService.getEPRuntime().sendEvent(orderEvent34);
				epService.getEPRuntime().sendEvent(orderEvent35);
				epService.getEPRuntime().sendEvent(orderEvent36);
				epService.getEPRuntime().sendEvent(orderEvent37);
				epService.getEPRuntime().sendEvent(orderEvent38);
				epService.getEPRuntime().sendEvent(orderEvent39);
				epService.getEPRuntime().sendEvent(orderEvent310);
				epService.getEPRuntime().sendEvent(orderEvent311);
				epService.getEPRuntime().sendEvent(orderEvent312);
				*/
	}

}
